<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="仓库" prop="warehouse">
        <el-select v-model="queryParams.warehouse" placeholder="请选择仓库" clearable size="small">
          <el-option
            v-for="dict in warehouseOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="类型" prop="productTypes">
        <el-cascader
          :options="typeList1"
          :props="optionProps"
          v-model="productType"
          @change="handleChange"
          clearable
          style="width: 300px;margin-bottom:10px"
        >
        </el-cascader>
      </el-form-item>
      <el-form-item label="数量" prop="quantity">
        <el-input
          v-model="queryParams.quantity"
          placeholder="请输入数量"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="productList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="数量" align="center" prop="id" />
      <el-table-column label="仓库" align="center" prop="warehouse" :formatter="warehouseFormat" />
      <el-table-column label="类型名称" align="center" prop="productTypeName">
        <template slot-scope="scope">
          <span>{{scope.row.productTypeName.slice(0,scope.row.productTypeName.length-1)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="数量" align="center" prop="quantity" />
      <el-table-column label="创建日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button> -->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleOut(scope.row)"
          >出库</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库产品对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库" prop="warehouse">
          <el-select v-model="form.warehouse" placeholder="请选择仓库">
            <el-option
              v-for="dict in warehouseOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="productTypes">
          <el-cascader
              :options="typeList1"
              :props="optionProps"
              v-model="form.productTypes"
              style="width: 300px;"
              >
          </el-cascader>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form.quantity" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="产品出库" :visible.sync="open_out" width="800px" append-to-body>
      <el-form ref="form_out" :model="form_out" :rules="rules" label-width="80px">
        <el-form-item label="仓库">
          <el-select v-model="form_out.warehouse" placeholder="请选择仓库">
            <el-option
              v-for="dict in warehouseOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型">
          <!-- <el-input v-model="form.productType" placeholder="请输入类型id" /> -->
          <el-cascader
              :options="typeList1"
              :props="optionProps"
              v-model="form_out.productTypes"
              style="width: 300px;"
              >
          </el-cascader>
        </el-form-item>
        <el-form-item label="数量" prop="quantity">
          <el-input v-model="form_out.quantity" placeholder="请输入数量" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm_out">确 定</el-button>
        <el-button @click="open_out=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProduct, getProduct, delProduct, addProduct, updateProduct, exportProduct,outProduct } from "@/api/warehouse/index";
import { listDept1} from "@/api/warehouse/index";

export default {
  name: "Product",
  data() {
    return {
      productType:[],
      optionProps: {
        value: 'id',
        label: 'label',
        children: 'children',
      },
      form_out:{},
      open_out:false,
      type1:undefined,
      typeList1:[],
      type2:undefined,
      typeList2:[],
      type3:undefined,
      typeList3:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 仓库产品表格数据
      productList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rules:{
        warehouse: [
          { required: true, message: "请选择仓库", trigger: "blur" }
        ],
        productTypes: [
          { required: true, message: "请选择类型", trigger: "blur" }
        ],
        quantity: [
          { required: true, message: "请输入数量", trigger: "blur" }
        ],
      },
      // 仓库字典
      warehouseOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouse: undefined,
        productTypes: [],
        productTypeName: undefined,
        quantity: undefined,
        productType:undefined
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
    this.getDicts("wareHouse").then(response => {
      this.warehouseOptions = response.data;
    });
  },
  methods: {
    handleChange(e){
      console.log(e)
    },
    select1(e){
      console.log(e);
      this.select1=e;
      this.typeList2 = undefined
      // listDept({
      //     parentId:e
      //   }).then(res => {
      //     this.typeList2 = res.rows
      //   });
    },
    select2(e){
      console.log(e);
      this.select2=e;
      listDept({
          parentId:e
        }).then(res => {
          this.typeList3 = res.rows
        });
    },
    select3(e){
      console.log(e);
      this.select3=e;
    },
    /** 查询仓库产品列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then(response => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
        listDept1().then(res => {
          this.typeList1 = res.data
          // 转化成树结构
          // this.typeList1 = this.handleTree(res.rows, "id");
          console.log(this.typeList1)
        });
      });
    },
    // 仓库字典翻译
    warehouseFormat(row, column) {
      return this.selectDictLabel(this.warehouseOptions, row.warehouse);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        warehouse: undefined,
        productType: undefined,
        productTypeName: undefined,
        quantity: undefined,
        createTime: undefined,
        updateTime: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      console.log(this.queryParams)
      this.queryParams.pageNum = 1;
      this.queryParams.productType=this.productType.join(',')
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "产品入库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getProduct(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "产品修改";
      });
    },
    handleOut(row) {
      this.form_out={};
      const id = row.id;
      getProduct(id).then(response => {
        this.form_out = response.data;
        this.form_out.quantity=undefined;
        this.open_out = true;
      });
    },
    submitForm_out(){
      this.$refs["form_out"].validate(valid => {
        if (valid) {
          if(this.form_out.quantity==0){
            this.$message.error('数量不能为0');
            return false
          };
          outProduct({
            id:this.form_out.id,
            num:this.form_out.quantity
          }).then(res=>{
            console.log(res)
            this.msgSuccess('出库成功');
            this.open_out=false;
            this.getList()
          })
        }
      })
    },
    /** 提交按钮 */
    submitForm: function() {

      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateProduct(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addProduct(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除该仓库产品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProduct(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有仓库产品数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProduct(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style lang="scss">
  .list_type{
    display: inline-block;
    margin-right: 10px;
  }
</style>